#include "iic.h"
#define __AT24C16_C__
#include "at24c16.h"
#define DeviceADD_EEPROM 0xa0
unsigned char read_24c16(unsigned short address,unsigned char *datbuf,unsigned short datl)
{
	unsigned char Address_low[2];
	U32 timer;
	if(datl<1)return ERROR;
	Address_low[1]=(unsigned char)address;
	Address_low[0]=((unsigned char)(address>>8))&0x07;
	Address_low[0]=DeviceADD_EEPROM|(Address_low[0]<<1);
	Address_low[0]&=0xfe;
	if(OK!=iic_start())
	{
		for(timer=0;timer<1000000;timer++);
		return ERROR;
	}
	if(OK!=iic_send_dat(Address_low,2))
	{
		iic_stop();
		for(timer=0;timer<1000000;timer++);
		return ERROR;
	}
	if(OK!=iic_restart())
	{
		iic_stop();
		for(timer=0;timer<1000000;timer++);
		return ERROR;
	}
	Address_low[0]|=1;
	if(OK!=iic_rece_dat(Address_low[0],datbuf,datl))
	{
		iic_rstop();
		for(timer=0;timer<1000000;timer++);
		return ERROR;
	}
	else
	{
		iic_rstop();
		for(timer=0;timer<1000000;timer++);
		return OK;
	}
}

unsigned char write_24c16(unsigned short address,unsigned char *datbuf,unsigned short datl)
{
	unsigned char Address_low[2];
	U32 timer;
	Address_low[1]=(unsigned char)address;
	Address_low[0]=((unsigned char)(address>>8))&0x07;
	Address_low[0]=DeviceADD_EEPROM|(Address_low[0]<<1);
	Address_low[0]&=0xfe;
	if(OK!=iic_start())return ERROR;
	if(OK!=iic_send_dat(Address_low,2))
	{
		iic_stop();
		return ERROR;
	}
	if(OK!=iic_send_dat(datbuf,datl))
	{
		iic_stop();
		return ERROR;
	}
	iic_stop();
	for(timer=0;timer<1000000;timer++);
	return OK;
}